package beastick.utility;

//Copyright 2008 Tofig Kareemov, estereos@gmail.com
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
//http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.

public class CStringArray extends CUtilityMethods
{
  private String[] sContent = null;
  private int iLast = 0;

  public CStringArray()
  {
    super();
    Reset(1);
  }

  public CStringArray(int iCapacity)
  {
    super();
    Reset(iCapacity);
  }

  public void Reset(int iCapacity)
  {
    iCapacity = MinLimited(iCapacity, 1);
    sContent = new String[iCapacity];
    iLast = -1;
  }

  public int Length()
  {
    return iLast + 1;
  };

  public String[] ToArray()
  {
    String[] sTempContent = new String[iLast + 1];
    int i = 0;

    for (i = 0; i < sTempContent.length; ++i)
    {
      sTempContent[i] = sContent[i];
    };
    return sTempContent;
  }

  public void Append(String sInput)
  {
    String[] sTempContent = null;
    int i = 0;

    ++iLast;
    if ((sContent.length - 1) <= iLast)
    {
      sTempContent = new String[sContent.length];
      for (i = 0; i < sContent.length; ++i)
      {
        sTempContent[i] = sContent[i];
      };
      sContent = new String[sContent.length * 2 + 1];
      for (i = 0; i < sTempContent.length; ++i)
      {
        sContent[i] = sTempContent[i];
      };
    }
    sContent[iLast] = sInput;
  }

  public void Append(String[] sInput)
  {
    int i = 0;

    for (i = 0; i < sInput.length; ++i)
    {
      Append(sInput[i]);
    };
  }

  public void AppendValuable(String[] sInput)
  {
    int i = 0;

    for (i = 0; i < sInput.length; ++i)
    {
      if (IsValuable(sInput[i]))
      {
        Append(sInput[i]);
      };
    };
  }

  public String GetItem(int iIndex)
  {
    String bResult = "";

    if (IsInRange(iIndex, 0, iLast))
    {
      bResult = sContent[iIndex];
    };
    return bResult;
  }

  public String GetLastItem()
  {
    return GetItem(iLast);
  }

  public void SetItem(int iIndex, String sValue)
  {
    if (IsInRange(iIndex, 0, iLast))
    {
      sContent[iIndex] = sValue;
    };
  };

  public int FoundItemIndex(int iIndex, String sValue)
  {
    int iResult = -1;
    int i = 0;

    if (IsInRange(iIndex, 0, iLast))
    {
      for (i = 0; i < iLast; ++i)
      {
        if (sContent[i].equals(sValue))
        {
          iResult = i;
          i = iLast;
        }
      }

    };
    return iResult;
  };

}
